package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.dtos.PageResponseResult;
import com.heima.model.admin.dtos.ResponseResult;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
@Slf4j
class ChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements ChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //检查参数
        if (dto == null) {
            return ResponseResult.errorResult ( AppHttpCodeEnum.PARAM_INVALID );
        }
        //封装参数
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery ();
        if (StringUtils.isNotBlank ( dto.getName () )) {
            queryWrapper.like ( AdChannel::getName, dto.getName () );
        }
        //分页参数
        dto.checkParam ();
        IPage pageReq = new Page ( dto.getPage (), dto.getSize () );
        //条件查询
        IPage<AdChannel> pageResult = this.page ( pageReq, queryWrapper );

        //封装响应结果
        PageResponseResult pageResponseResult = new PageResponseResult ( dto.getPage (), dto.getSize (), pageReq.getTotal () );
        pageResponseResult.setData ( pageResult.getRecords () );

        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        //检查参数
        if (channel == null && StringUtils.isNotBlank ( channel.getName () )) {
            return ResponseResult.errorResult ( AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空" );
        }
        int count = this.count ( Wrappers.<AdChannel>lambdaQuery ().eq ( AdChannel::getName, channel.getName () ) );
        if (count > 0) {
            return ResponseResult.errorResult ( AppHttpCodeEnum.DATA_EXIST, "频道已存在" );
        }
        //检查数据库数据是否存在
        this.save ( channel );
        //返回结果
        return ResponseResult.okResult ();
    }

    @Override
    public ResponseResult update(AdChannel channel) {
        //检查参数 id
        if (channel == null || channel.getId () == null) {
            return ResponseResult.errorResult ( AppHttpCodeEnum.PARAM_INVALID, "频道ID不能为空" );
        }
        AdChannel byId = this.getById ( channel.getId () );
        if (byId == null) {
            return ResponseResult.errorResult ( AppHttpCodeEnum.DATA_EXIST, "频道不存在" );
        }
        //根据频道查询id
        if (StringUtils.isNotBlank ( channel.getName () ) || !channel.getName ().equals ( byId.getName () )) {
            int count = this.count ( Wrappers.<AdChannel>lambdaQuery ().eq ( AdChannel::getName, channel.getName () ) );
            //检查频道是否存在
            if (count > 0) {
                return ResponseResult.errorResult ( AppHttpCodeEnum.DATA_EXIST, "频道已存在" );
            }
        }


        //修改频道
        this.updateById ( channel );
        return ResponseResult.okResult ();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = getById(id);
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 启用状态下不能删除
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //3.删除频道
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
